﻿using Microsoft.EntityFrameworkCore;
using WMS.FC.Domain.PersistenceObject.System;
using WMS.FC.Domain.Repository;
using WMS.FC.Infrastructure.Common;

namespace WMS.FC.Infrastructure.Repository
{
    public class MenuRepository : RepositoryBase, IMenuRepository
    {
        public MenuRepository(WmsDbContext dbContext) : base(dbContext)
        {
        }

        public async Task<List<MenuPO>> GetMenuList()
        {
            return await dbContext.Set<MenuPO>().AsNoTrackingWithIdentityResolution().Where(entity => entity.TypeCode == "MenuType_Menu").ToListAsync();
        }

        public async Task<List<MenuPO>> GetAllMenuList()
        {
            return await dbContext.Set<MenuPO>().AsNoTrackingWithIdentityResolution().ToListAsync();
        }

        public async Task<MenuPO?> GetMenu(long id)
        {
            return await dbContext.Set<MenuPO>().AsNoTrackingWithIdentityResolution().AsNoTrackingWithIdentityResolution().FirstOrDefaultAsync(entity => entity.Id == id);
        }

        public async Task<MenuPO?> GetMenuWithSubMenu(long id)
        {
            return await dbContext.Set<MenuPO>().AsNoTrackingWithIdentityResolution().Include(entity => entity.SubMenu).AsSplitQuery().FirstOrDefaultAsync(entity => entity.Id == id);
        }

        public async Task<MenuPO?> GetMenuWithParentMenu(long id)
        {
            return await dbContext.Set<MenuPO>().AsNoTrackingWithIdentityResolution().Include(entity => entity.Parent).AsSplitQuery().FirstOrDefaultAsync(entity => entity.Id == id);
        }
    }
}
